<?php

namespace App\Model;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\Model;
use PhpParser\Error;

class User extends Model
{
//    protected $hidden = ['password'];
    //
    protected $table = 'users';

    public $timestamps = false;

    protected $primaryKey = 'email';

    protected $keyType = 'string';

    protected $fillable = ['email','root','password'];

    public function setPasswordAttribute($value){
//        $this -> attributes['password'] = md5($value);
        $this -> attributes['password'] = $value;
    }

    /**
     * 账号密码是否正确
     * @author
     * @date xxx
     */
    public function check($param){
        if($param->get('email') === $this ->email && $param->get('password') === $this ->password ){
            $token = uniqid();
            session(['user' => [
                'token' => $token,
                'root' =>  $this->root
            ]]);
//            session(['token' => $token]);
            return response(['msg' => '登录成功','token' => $token,'code' => 200,'email' => $this ->email],200)->cookie('token', $token,120);
        }else {

            return response(['msg' => '密码不正确','code' => 700]);
        }
    }
}
